rm(list=ls())

# Data --------------------------------------------------------------------

partydata <- readRDS("vparty_subset.rds") %>% 
  rename(party_name = v2paenname,
         seat_share = v2paseatshare,
         vote_share = v2pavote,
         anti_pluralism = v2xpa_antiplural,
         clientelism = v2paclient,
         government_support = v2pagovsup
         ) %>% 
  mutate(country_party = paste(country_name,sep="_",party_name)) %>% 
  select(-party_name,-country_name,-v2paopresp_nr,-v2paplur_nr,-v2paminor_nr,-v2paviol_nr)

# Create annual data frame ------------------------------------------------

years <- c(1980:2019)
parties <- levels(as.factor(partydata$country_party))
partydata_annual <- expand.grid(years,parties) %>% 
  rename(year = Var1,
         country_party = Var2) %>% 
  separate(country_party,c("country","party"),sep="_",remove=FALSE) %>% 
  filter(country != "German Democratic Republic")

partydata_annual %>% 
  left_join(partydata, by = c("country_party","year")) ->
  party_annual_timeseries

party_annual_timeseries %>% 
  mutate(antipluralism_gov_party = case_when(government_support %in% c(0,1) ~ anti_pluralism,
                                             TRUE ~ NA_real_),
         antipluralism_head_party = case_when(government_support == 0 ~ anti_pluralism,
                                             TRUE ~ NA_real_),
         clientelism_gov_party = case_when(government_support %in% c(0,1) ~ clientelism,
                                           TRUE ~ NA_real_)) %>% 
  group_by(country,year) %>%
  summarise(antipluralism_gov_seat_share = weighted.mean(antipluralism_gov_party,seat_share,na.rm = TRUE),
            antipluralism_gov_vote_share = weighted.mean(antipluralism_gov_party,vote_share,na.rm = TRUE),
            antipluralism_gov_head_seat_share = weighted.mean(antipluralism_head_party,seat_share,na.rm = TRUE),
            clientelism_gov_seat_share = weighted.mean(clientelism_gov_party,seat_share,na.rm = TRUE)
            ) %>%
  ungroup()  %>%
  mutate(across(c(antipluralism_gov_seat_share,antipluralism_gov_vote_share,antipluralism_gov_head_seat_share,clientelism_gov_seat_share),
                    ~ ifelse(. == "NaN", NA_real_, .))) %>% 
  group_by(country) %>%
  fill(antipluralism_gov_seat_share,
       antipluralism_gov_vote_share,
       antipluralism_gov_head_seat_share,
       clientelism_gov_seat_share)  %>%
  ungroup()  %>%
  filter(year >= 1990) ->
  party_annual_timeseries_filled

saveRDS(party_annual_timeseries_filled, file="party_time_series.rds")


# Reduced data ------------------------------------------------------------

# Data --------------------------------------------------------------------

partydata <- readRDS("vparty_subset.rds") %>% 
  rename(party_name = v2paenname,
         seat_share = v2paseatshare,
         vote_share = v2pavote,
         anti_pluralism = v2xpa_antiplural,
         clientelism = v2paclient,
         government_support = v2pagovsup
  ) %>% 
  mutate(country_party = paste(country_name,sep="_",party_name)) %>% 
  filter(v2paopresp_nr > 3 &  v2paplur_nr > 3 & v2paminor_nr > 3 & v2paviol_nr > 3) %>%
  select(-party_name,-country_name,-v2paopresp_nr,-v2paplur_nr,-v2paminor_nr,-v2paviol_nr)

# Create annual data frame ------------------------------------------------

years <- c(1980:2019)
parties <- levels(as.factor(partydata$country_party))
partydata_annual <- expand.grid(years,parties) %>% 
  rename(year = Var1,
         country_party = Var2) %>% 
  separate(country_party,c("country","party"),sep="_",remove=FALSE) %>% 
  filter(country != "German Democratic Republic")

partydata_annual %>% 
  left_join(partydata, by = c("country_party","year")) ->
  party_annual_timeseries

party_annual_timeseries %>% 
  mutate(antipluralism_gov_party = case_when(government_support %in% c(0,1) ~ anti_pluralism,
                                             TRUE ~ NA_real_),
         antipluralism_head_party = case_when(government_support == 0 ~ anti_pluralism,
                                              TRUE ~ NA_real_),
         clientelism_gov_party = case_when(government_support %in% c(0,1) ~ clientelism,
                                           TRUE ~ NA_real_)) %>% 
  group_by(country,year) %>%
  summarise(antipluralism_gov_seat_share = weighted.mean(antipluralism_gov_party,seat_share,na.rm = TRUE),
            antipluralism_gov_vote_share = weighted.mean(antipluralism_gov_party,vote_share,na.rm = TRUE),
            antipluralism_gov_head_seat_share = weighted.mean(antipluralism_head_party,seat_share,na.rm = TRUE),
            clientelism_gov_seat_share = weighted.mean(clientelism_gov_party,seat_share,na.rm = TRUE)
  ) %>%
  ungroup()  %>%
  mutate(across(c(antipluralism_gov_seat_share,antipluralism_gov_vote_share,antipluralism_gov_head_seat_share,clientelism_gov_seat_share),
                ~ ifelse(. == "NaN", NA_real_, .))) %>% 
  group_by(country) %>%
  fill(antipluralism_gov_seat_share,
       antipluralism_gov_vote_share,
       antipluralism_gov_head_seat_share,
       clientelism_gov_seat_share)  %>%
  ungroup()  %>%
  filter(year >= 1990) ->
  party_annual_timeseries_filled

saveRDS(party_annual_timeseries_filled, file="party_time_series_reduced.rds")